package category.client;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ProductDetail extends Composite{
	VerticalPanel page = new VerticalPanel();
	public ProductDetail() {
		super();
	}

	public void getProductDetail(String pid) {
		String url = GWT.getModuleBaseURL() + URL.encode("getDetailService") + URL.encode("?id="+pid);
		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,url);
		try {
			builder.sendRequest(null, new GetDetailRequestCallback());
		} catch (RequestException e) {
		}
	}
	private void listDetail(Map<String,String> map){
			page.clear();
			FlexTable ret = new FlexTable();
			ret.setWidget(0, 0, new HTML("<img src='/static/img/products/detail/"+(String)map.get("ID")+".jpg'>"));//new Image("/static/img/products/detail/"+(String)map.get("ID")+".jpg"));
			ret.setWidget(1, 0, new HTML("<p align='center'><strong>产品名称:"+(String)map.get("Title")+"</strong></p>"));
			ret.setWidget(2, 0, new HTML("<p align='left'><strong>产品属性:</strong></p>"));
			map.remove("Title");
			map.remove("ID");
			FlexTable grid = new FlexTable();
			grid.setBorderWidth(1);
			int row=0;
			for(String s:map.keySet()){
				grid.setHTML(row, 0, "<span style='font-size:12;'>"+s+"</span>");
				grid.getCellFormatter().setWidth(row, 0, "80px");
				grid.getCellFormatter().setAlignment(row, 0, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP);
				grid.setHTML(row, 1, "<span style=font-size:12;'>"+(String)map.get(s)+"</span>");
				grid.getCellFormatter().setAlignment(row, 1, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP);
				row++;
			}
			ret.setWidget(3, 0, grid);
			ret.getFlexCellFormatter().setAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER,HasVerticalAlignment.ALIGN_BOTTOM);
			page.add(ret);
			page.add(new HTML("<p align=right><a href='/products.jhtml'><-返回</a></p>"));
	}

		private class GetDetailRequestCallback implements RequestCallback {
			public GetDetailRequestCallback() {
				super();
			}
			public void onError(Request request, Throwable exception) {
			}

			public void onResponseReceived(Request request, Response response) {
			   if (200 == response.getStatusCode()) {
			    	  String respText = response.getText();
			    	  if(respText.equals(""))return;
			    	  Map<String,String> map = new HashMap<String,String>();
						JSONValue respValue = JSONParser.parseLenient(respText);
						JSONArray trArray = respValue.isArray();
						if(trArray.size()>0){
							for(int i=0;i<trArray.size();i++){
								JSONObject jo = trArray.get(i).isObject();
								String name= jo.get("AttrName").isString().stringValue();
								String value= jo.get("AttrValue").isString().stringValue();
								map.put(name, value);
							}
						}
						listDetail(map);
		      } 
			}
		}
}
